package com.jcl.recruit.wechat.mapper;

import com.jcl.recruit.wechat.model.WeixinConfig;

import static org.apache.ibatis.jdbc.SqlBuilder.*;

public class WeixinConfigProvider {

    public String insertProvider(WeixinConfig config){
        BEGIN();
        INSERT_INTO("weixin_config");
        if(null != config.getAppId()){
            VALUES("app_id", "#{appId,jdbcType=VARCHAR}");
        }
        if(null != config.getAppSecret()){
            VALUES("app_secret", "#{appSecret,jdbcType=VARCHAR}");
        }
        if(null != config.getAccessToken()){
            VALUES("access_token", "#{accessToken,jdbcType=VARCHAR}");
        }
        if(null != config.getTicket()){
            VALUES("ticket", "#{ticket,jdbcType=VARCHAR}");
        }
        if(null != config.getTokenOverTime()){
            VALUES("token_over_time", "#{tokenOverTime,jdbcType=INTEGER}");
        }
        if(null != config.getTicketOverTime()){
            VALUES("ticket_over_time", "#{ticketOverTime,jdbcType=INTEGER}");
        }
        if(null != config.getTokenUpdateTime()){
            VALUES("token_update_time", "#{tokenUpdateTime,jdbcType=TIMESTAMP}");
        }
        if(null != config.getTicketUpdateTime()){
            VALUES("ticket_update_time", "#{ticketUpdateTime,jdbcType=TIMESTAMP}");
        }
        return SQL();
    }

    public String updateProvider(WeixinConfig config){
        BEGIN();
        UPDATE("weixin_config");
        if(null != config.getAppId()){
            SET("app_id=#{appId}");
        }
        if(null != config.getAppSecret()){
            SET("app_secret=#{appSecret}");
        }
        if(null != config.getAccessToken()){
            SET("access_token=#{accessToken}");
            SET("token_update_time=now()");
        }
        if(null != config.getTicket()){
            SET("ticket=#{ticket}");
            SET("ticket_update_time=now()");
        }
        if(null != config.getTokenOverTime()){
            SET("token_over_time=#{tokenOverTime}");
        }
        if(null != config.getTicketOverTime()){
            SET("ticket_over_time=#{ticketOverTime}");
        }

        WHERE("id = #{id,jdbcType=INTEGER}");
        return SQL();
    }
}
